package io.github.bloquesoft.entity.clazz.value;

import io.github.bloquesoft.entity.clazz.ClassEntityDefinition;
import io.github.bloquesoft.entity.definition.EntityDefinition;
import io.github.bloquesoft.entity.value.EntityValue;
import io.github.bloquesoft.entity.value.EntityValueFactory;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ObjectEntityValueFactory implements EntityValueFactory {

    @Override
    public EntityValue create(EntityDefinition entityDefinition, Object entityObject, EntityValue parent) {

        if (!(entityDefinition instanceof ClassEntityDefinition)) {
            log.warn("entityDefinition must be ClassEntityDefinition, entityDefinition id: " + entityDefinition.getId());
            throw new IllegalArgumentException("entityDefinition must be ClassEntityDefinition, entityDefinition id: " + entityDefinition.getId());
        }
        if ( parent != null && !(parent instanceof ObjectEntityValue))
        {
            log.warn("parent must be ObjectEntityValue, parent entity definition: " + parent.getEntityDefinition().getId());
            throw new IllegalArgumentException("parent must be ObjectEntityValue, parent entity definition: " + parent.getEntityDefinition().getId());
        }
        return new ObjectEntityValue((ClassEntityDefinition)entityDefinition, entityObject, (ObjectEntityValue) parent);
    }
}